﻿using UnityEngine;
using System.Collections;

public class Selection : MonoBehaviour {

    public Player player;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // test for ui

            // else
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (hit.transform.GetComponent<BaseUnit>())
                {
                    player.selected.Clear();
                    player.selected.Add(hit.transform.GetComponent<BaseUnit>());
                }
            }
        }
        if (Input.GetMouseButtonDown(1))
        {
            // test for ui

            // else
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                if (hit.transform.GetComponent<Destructible>())
                {
                    foreach (var u in player.selected)
                        u.target = hit.transform.GetComponent<Destructible>();
                }
                else
                {
                    foreach (var u in player.selected)
                        u.Goto(hit.point);
                }
            }
        }
	}
}
